package com.sdd.rpi_mobile.eventRss;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import android.util.Log;

/**
 * This class is the RSSItem object containing the correct fields. 
 * @author Norbert
 *
 */
public class RssItem implements Serializable{
	private static final long serialVersionUID = 1L;
	private String mTitle;
	private String mLink;
	private String mDescription;
	private String mAuthor;
	private ArrayList<String> mCategories = new ArrayList<String>();
	private String mGuid;
	private Date mPubDate;
	
	//The setter method for all fields in the RSSItem object.
	public boolean setElement(String element, String data) {
		if( element.equals("title")) {
			this.mTitle = data;
			return true;
		}
		else if(element.equals("link")) {
			this.mLink = data;
			return true;
		}
		else if(element.equals("description")) {
			this.mDescription = parseDescription(data);
			return true;
		}
		else if( element.equals("pubDate") ) {
			this.setDate(data);
			return true;
		}
		else if(element.equals("author") ) {
			this.mAuthor = data;
			return true;
		}
		else if(element.equals("category")) {
			this.mCategories.add(data);
			return true;
		}
		else if(element.equals("guid")) {
			this.mGuid = data;
			return true;
		}
		else {
			return false;
	    }
	}
	
	/**
	 * Parses the description, in order to handle new lines etc.
	 * @param data
	 * @return
	 */
	private String parseDescription(String data) {
		Log.v("RSS", data);
		String desc = data.replaceAll("\\r\\n|\\r|\\n", " ");;
		desc = desc.replace("<br/>", "\n");
		desc = desc.replaceAll("[ \t]{2,}", " ");
		return desc;
	}
	
	/**
	 * Parses a String containing the date
	 * @param date
	 * String containing a date in the UTC timezone formatted as 03 Apr 2013 23:12:12
	 */
	private void setDate(String date) {
		SimpleDateFormat dateParser = new SimpleDateFormat("dd MMM yyyy kk:mm:ss", Locale.ENGLISH);
		dateParser.setTimeZone(TimeZone.getTimeZone("UTC"));
		try {
			this.mPubDate = dateParser.parse(date);
		} catch (ParseException e) {
			this.mPubDate = null;
			Log.e("RSS Date", "Unable to parse " + date);
		}
	}
	
	//Getter methods for every field.
	public String getTitle(){ return this.mTitle; }
	public String getLink(){ return this.mLink; }
	public String getDescription(){ return this.mDescription; }
	public String getAuthor(){ return this.mAuthor; }
	public ArrayList<String> getCategories(){ return this.mCategories; }
	public String getGuid(){ return this.mGuid; }
	public Date getPubDate(){ return this.mPubDate; }
	
	@Override
	public String toString() {
		String itemString = "";
		itemString += this.mTitle + '\n';
		itemString += this.mLink + '\n';
		itemString += this.mPubDate.toString() + '\n';
		itemString += this.mDescription + '\n';
		itemString += "In categories: ";
		for(String category: this.mCategories) itemString += category + '\n';
		return itemString;
	}
}